package com.jvyou.driver.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.jvyou.driver.domain.modal.Result;
import com.jvyou.driver.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 拦截：未登录异常
    @ExceptionHandler(NotLoginException.class)
    public Result<Void> handlerException(NotLoginException e) {
        log.error("未登录异常：{}", e.getMessage());
        return Result.errorResult(AppHttpCodeEnum.REQUIRE_TOKEN);
    }

    // 拦截：缺少权限异常
    @ExceptionHandler(NotPermissionException.class)
    public Result<Void> handlerException(NotPermissionException e) {
        log.error("缺少权限异常：{}", e.getMessage());
        return Result.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
    }

    // 拦截：缺少角色异常
    @ExceptionHandler(NotRoleException.class)
    public Result<Void> handlerException(NotRoleException e) {
        log.error("缺少角色异常：{}", e.getMessage());
        return Result.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
    }

    // 全局异常拦截 
    @ExceptionHandler
    public Result<Void> handlerException(Exception e) {
        return Result.errorResult(AppHttpCodeEnum.UNKNOWN_ERROR, e.getMessage());
    }
}
